﻿Namespace CommandBlocks
    Public Class Authenticate
        Public Class Request
            Public PassCode As String
            Public Name As String
            Public Email As String
            Public UniqueId As String
            Public Version As String
        End Class
        Public Class Reply
            Public Success As Boolean
            Public Message As String
            Public PassCode As String
            Public Name As String
            Public Email As String
            Public UniqueId As String
            Public Host As String
            Public Port As Integer
            Public OneTimeDataChannelPassCode As String
            Public Version As String
        End Class

        Friend Shared Function HandleRequest(ByVal node As Node, ByVal commandBlock As MessageBlock) As Boolean
            Dim clsRequest As Request = Framework.Serialization.FromXmlBytes(Of Request)(commandBlock.Message)

            Dim clsBud As Entities.Buddy = Framework.Buddies.Manager.GetByUniqueId(clsRequest.UniqueId)
            If clsBud Is Nothing Then
                Manager.OnNodeLogEvent(node, String.Format("Buddy with id ""{0}"" does not exist", clsRequest.UniqueId))
                Return False
            End If
            If Not clsBud.PassCode_LoginHere = clsRequest.PassCode Then
                Manager.OnNodeLogEvent(node, String.Format("Buddy id ""{0}"" pass code does not match", clsRequest.UniqueId))
                Return False
            End If

            clsBud.LastConnected = Now.ToUniversalTime
            clsBud.Name = clsRequest.Name
            clsBud.Email = clsRequest.Email
            node.Version = clsRequest.Version
            node.Buddy = clsBud

            Framework.Buddies.Manager.Save(clsBud)

            Dim clsReply As New Reply
            clsReply.Success = True
            clsReply.Email = Framework.Net.Settings.Email
            clsReply.Name = Framework.Net.Settings.Nickname
            clsReply.PassCode = clsBud.PassCode_LoginAtBuddy
            clsReply.UniqueId = Framework.Net.Settings.UniqueId
            clsReply.Host = Framework.Net.Settings.Host
            clsReply.Port = Framework.Net.Settings.Port
            clsReply.OneTimeDataChannelPassCode = Framework.Misc.GeneratePassCode(256)
            clsReply.Version = System.Windows.Forms.Application.ProductVersion
            node.OneTimeDataChannelPassCode = clsReply.OneTimeDataChannelPassCode

            node.PostCommandMessage("AUTHENTICATEREPLY", Framework.Serialization.ToXmlBytes(clsReply))

            Dim strReply As String = node.ReadCommandLine(30)
            If strReply = "AUTHENTICATED" Then
                Manager.OnNodeLogEvent(node, "Connection successful")
            Else
                Manager.OnNodeLogEvent(node, "Did not succeed in authentication")
            End If
            Return True
        End Function

        Friend Shared Function HandleReply(ByVal node As Node, ByVal commandBlock As MessageBlock) As Boolean
            Dim clsArgs As Reply = Framework.Serialization.FromXmlBytes(Of Reply)(commandBlock.Message)

            If Not clsArgs.Success Then
                Manager.OnNodeLogEvent(node, "Authenticate failed")
                Return False
            End If

            If node.Buddy.UniqueId <> clsArgs.UniqueId Then
                Manager.OnNodeLogEvent(node, "Unique Id did not match!")
                Return False
            End If
            If node.Buddy.PassCode_LoginHere <> clsArgs.PassCode Then
                Manager.OnNodeLogEvent(node, "Passcode did not match!")
                Return False
            End If

            node.Buddy.Email = clsArgs.Email
            node.Buddy.Host = clsArgs.Host
            node.Buddy.LastConnected = Now.ToUniversalTime
            node.Buddy.Name = clsArgs.Name
            node.Buddy.Port = clsArgs.Port

            Framework.Buddies.Manager.Save(node.Buddy)

            node.Version = clsArgs.Version
            'send "AUTHENTICATED"
            node.OneTimeDataChannelPassCode = clsArgs.OneTimeDataChannelPassCode
            node.SendCommandLine("AUTHENTICATED")
            Return True
        End Function

    End Class



End Namespace